package com.king.springcloud.controller;

import com.king.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author king
 * @date 2020/9/6 22:36
 */
@Slf4j
@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    /**
     * 正常访问
     * @param id id
     * @return 结果
     */
    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_OK(id);
        log.info("ok: {}", result);
        return result + " 端口号：" + serverPort;
    }

    /**
     * 异常访问
     * @param id id
     * @return 结果
     */
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeout(@PathVariable("id")Integer id){
        String result = paymentService.paymentInfo_Timeout(id);
        log.info("timeout: {}", result);
        return result + " 端口号：" + serverPort;
    }

    /**
     * 超时补救
     *  HystrixCommand
     *      fallbackMethod 出错调用补救方法
     *      commandProperties
     *          HystrixProperty 响应峰值设置, 超过则调用补救方法
     * @param id id
     * @return 结果
     */
    @GetMapping("/payment/hystrix/timeout3/{id}")
    public String paymentInfoTimeout3(@PathVariable("id")Integer id) {
        return paymentService.paymentInfo_Timeout3(id);
    }

    /**
     * 正常延时
     *  HystrixCommand
     *      fallbackMethod 出错调用补救方法
     *      commandProperties
     *          HystrixProperty 响应峰值设置, 超过则调用补救方法
     * @param id id
     * @return 结果
     */
    @GetMapping("/payment/hystrix/timeout5/{id}")
    public String paymentInfoTimeout5(@PathVariable("id")Integer id){
        return paymentService.paymentInfo_Timeout5(id);
    }

    /**
     * 服务熔断
     * @param id 参数
     * @return 返回值
     */
    @GetMapping("/payment/hystrix/circuitBreaker/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        return paymentService.paymentCircuitBreaker(id);
    }

    /**
     * zipkin 测试用例
     * @return
     */
    @GetMapping("/payment/zipkin")
    public String paymentZipkin(){
        return "hi, i'am paymentzipkin server fall back";
    }
}
